var async = require('async');
var hotelService = rootRequire('services/hotelService')

module.exports =  function(server){
    server.route(
        [
            {
                method:'GET',path:"/hotel",
                handler:function(req,reply){
                    var hotelno = req.query['hn']
                    async.parallel({
                        hotel:function(next){
                            hotelService.getMainHotel(hotelno,function(err,body){

                                if(err)return next(err);

                                var info = body['hotel']
                                next(null,info)
                            })
                        },
                        banners:function(next){
                            hotelService.getBanners(hotelno,function(err,body){

                                if(err)return next(err)
                                var info = body['images']
                                next(null,info);
                            })
                        }
                    },function(err,results){
                        if(err){
                            //return reply.redirect('/404_api')
                            return reply(err)
                        }

                        var hotel = results.hotel,
                            banners = results.banners;

                        console.log(hotel)

                        var hotelDescription = hotel['hoHoteldes'] ||'酒店老板太赖' ;
                        var allDesc = ''

                        if(hotelDescription.length>100){
                            var allDesc = hotelDescription
                            hotelDescription = allDesc.substr(0,90);
                        }

                        return reply.view('hotel',{
                            title:'首页',
                            hotelName:hotel['hoHotelname'],
                            lgt :hotel['hoHotellongitude'],
                            lat:hotel['hoHotellatitude'],
                            allDesc:allDesc,
                            hotelDescription:hotelDescription,
                            active:{hotel:'active'},
                            hoWeb:hotel.hoWeb,
                            banners:banners,
                            hotelImg:hotel.hoHotelimg,

                            hotelNo:hotelno,
                            bookingUrl:'/booking?hn='+hotelno,
                            myUrl:'/my?hn='+hotelno
                        })
                    })
                }
            },
        ]
    )
}
